#!/usr/bin/env python
# encoding: utf-8


"""
@file: fangchengzu.py
@time: 2016/11/14 下午4:53
"""
# 三角方程组
from mathsolver.functions.base import *
from sympy import expand_trig, sin, cos, tan, simplify, symbols, cot, cancel, fraction
from itertools import chain
from mathsolver.functions.budengshi import common_opers as co


# 已知sinα+cosβ=\\frac{1}{3},sinβ-cosα=\\frac{1}{2},则sin(α-β)=()
# 只含有sin, cos的表达式
class FangChengZu001(BaseFunction):
    # 令w = sinα, x = cosα; y = sinβ, z = cosβ
    def solver(self, *args):
        self.label.add('解三角方程')
        eq1, eq2 = args[0].sympify()
        eq1_f = expand_trig(eq1[0] - eq1[1])
        eq2_f = expand_trig(eq2[0] - eq2[1])
        s1, s2 = sorted(set(chain(eq1_f.free_symbols, eq2_f.free_symbols)), key=str)
        w, xx, yy, z = symbols('w, x, y, z')
        symb_subs = ((sin(s1), w), (cos(s1), xx), (sin(s2), yy), (cos(s2), z))
        eq1_f_sub = eq1_f.subs(symb_subs)
        eq2_f_sub = eq2_f.subs(symb_subs)
        eqs_roots = co.solve_poly_eqs(
            BaseEqs([[eq1_f_sub, 0], [eq2_f_sub, 0], [w ** 2 + xx ** 2, 1], [yy ** 2 + z ** 2, 1]]))
        f = expand_trig(args[1].sympify())
        tan_cot_subs = ((tan(s1), w), (cot(s1), xx), (tan(s2), yy), (cot(s2), z))
        f = f.subs(tan_cot_subs)
        f_subs = f.subs(symb_subs)
        f_values = map(f_subs.subs, eqs_roots)
        f_values = list(set(map(simplify, f_values)))
        self.steps.append(['求得%s=%s' % (new_latex(f), new_latex(f_values[0])), ''])
        self.output.append(BaseNumber(f_values[0]))
        return self


# 已知cotα=2,tan(α-β)=-\\frac{2}{5},则tan(β-2α)=().
class FangChengZu002(BaseFunction):
    @staticmethod
    def std_f(f):
        mo, de = fraction(cancel(f))
        return mo

    def solver(self, *args):
        self.label.add('解三角方程')
        eq1, eq2 = args[0].sympify()
        eq1_f = expand_trig(eq1[0] - eq1[1])
        eq2_f = expand_trig(eq2[0] - eq2[1])
        s1, s2 = sorted(set(chain(eq1_f.free_symbols, eq2_f.free_symbols)), key=str)
        w, xx, yy, z = symbols('w, x, y, z')
        symb_subs = ((tan(s1), w), (cot(s1), xx), (tan(s2), yy), (cot(s2), z))
        eq1_f_sub = eq1_f.subs(symb_subs)
        eq1_f_sub = FangChengZu002.std_f(eq1_f_sub)
        eq2_f_sub = eq2_f.subs(symb_subs)
        eq2_f_sub = FangChengZu002.std_f(eq2_f_sub)
        eqs_roots = co.solve_poly_eqs(
            BaseEqs([[eq1_f_sub, 0], [eq2_f_sub, 0], [w * xx, 1], [yy * z, 1]]))
        f = expand_trig(args[1].sympify())
        f_subs = f.subs(symb_subs)
        f_values = map(f_subs.subs, eqs_roots)
        f_values = list(set(map(simplify, f_values)))
        self.steps.append(['求得%s=%s' % (new_latex(f), new_latex(f_values[0])), ''])
        self.output.append(BaseNumber(f_values[0]))
        return self


class SanJiaoFangChengZu(BaseFunction):
    CLS = [FangChengZu001, FangChengZu002]

    def solver(self, *args):
        r = None
        for cl in SanJiaoFangChengZu.CLS:
            try:
                r = cl(verbose=True).solver(*args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r
